//
//  WSAnswerViewController.m
//  WisdomStar
//
//  Created by 谭飞 on 2018/10/23.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "WSAnswerDisplayViewController.h"
#import "WSAnswerChildViewController.h"

#import "CourseService.h"

@interface WSAnswerDisplayViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIView *searchBar;

@property (weak, nonatomic) IBOutlet UITextField *searchText;

@property (nonatomic, strong) NSArray *datas;

@end

@implementation WSAnswerDisplayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self ws_setup];
    [self loadData];
}

- (void)ws_setup {
    // 设置搜索框
    self.searchBar.layer.cornerRadius = 19.f;
    self.searchBar.layer.borderWidth = 1.f;
    self.searchBar.layer.borderColor = kSeparatorColor.CGColor;
    
    WEAKSELF
    // 设置整体内容尺寸（包含标题滚动视图和底部内容滚动视图）
    [self setUpContentViewFrame:^(UIView *contentView) {
        
        CGFloat contentY = weakSelf.searchBar.xmg_height + TOPBARHEIGHT + 10;
        
        contentView.frame = CGRectMake(0, contentY, kMainScreenWidth, kMainScreenHeight - contentY);
        
    }];
    
    // 设置标题
    [self setUpTitleEffect:^(UIColor *__autoreleasing *titleScrollViewColor, UIColor *__autoreleasing *norColor, UIColor *__autoreleasing *selColor, UIFont *__autoreleasing *titleFont, CGFloat *titleHeight, CGFloat *titleWidth) {
        *norColor = kFontColorGray;
        *selColor = kFontColorGreen;
        *titleFont = WSFontMake(14);
    }];
    
    // 设置标题渐变
    // *推荐方式(设置标题渐变)
    [self setUpTitleGradient:^(YZTitleColorGradientStyle *titleColorGradientStyle, UIColor *__autoreleasing *norColor, UIColor *__autoreleasing *selColor) {
        // nothing
    }];
    
    // 设置下划线
    [self setUpUnderLineEffect:^(BOOL *isUnderLineDelayScroll, CGFloat *underLineH, UIColor *__autoreleasing *underLineColor,BOOL *isUnderLineEqualTitleWidth) {
        *underLineColor = kThemeGreenColor;
        *isUnderLineDelayScroll = YES;
        *isUnderLineEqualTitleWidth = YES;
    }];
    
}


/**
 加载顶部分类
 */
- (void)loadData {
    [CourseService fetchCourseCateWithResult:^(id  _Nonnull responseObj) {
        ResponseModel *response = [ResponseModel yy_modelWithJSON:responseObj];
        if (!response.data) {
            return ;
        }
        NSMutableArray *cateArr = [NSMutableArray array];
        [response.data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [cateArr addObject:obj];
        }];
        
        self.datas = [cateArr copy];
        
        // 移除之前所有子控制器
        [self.childViewControllers makeObjectsPerformSelector:@selector(removeFromParentViewController)];
        
        [self setUpAllViewController];
        
        // 注意：必须先确定子控制器
        [self refreshDisplay];
    }];
}


/**
 添加底部内容控制器
 */
- (void)setUpAllViewController {
    [self.datas enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        WSAnswerChildViewController *childVc = [[WSAnswerChildViewController alloc] init];
        childVc.title = obj[@"name"];
        childVc.cateId = obj[@"id"];
        childVc.yOffset = self.searchBar.xmg_bottom + 10.f;
        childVc.isLoad = idx == 0;
        [self addChildViewController:childVc];
    }];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField endEditing:YES];
    if (![textField hasText]) {
        return NO;
    }
    [kNotificationCenter postNotificationName:@"SearchNotification" object:textField.text];
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
